package com.example.demo.web.controller;

import com.example.demo.core.exception.ServiceException;
import com.example.demo.core.exception.StatusCode;
import com.example.demo.core.handler.exception.ExceptionInfoBO;
import com.example.demo.core.handler.exception.ResultBO;
import com.example.demo.core.handler.exception.ResultTool;
import com.example.demo.sys.service.UserService;
import com.example.demo.sys.service.annotation.UniqueUser;
import com.example.demo.sys.service.bo.UserBO;
import com.example.demo.sys.service.bo.UserProfileBO;
import com.example.demo.web.config.JwtConfig;
import com.example.demo.web.vo.UserProfileVO;
import com.example.demo.web.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Controller
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtConfig jwtConfig;

    /**
     * 需要 Token 验证的接口
     */
    @PostMapping("/info")
    @ResponseBody
    public ResultBO<?> info() {
        return ResultTool.success("info");
    }

    /**
     * 根据请求头的token获取userId
     *
     * @param request HttpServletRequest
     * @return ResultBO
     */
    @GetMapping("/getUserInfo")
    @ResponseBody
    public ResultBO<?> getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return ResultTool.error(new ExceptionInfoBO(403, "缺少token"));
        }
        String id = jwtConfig.getUsernameFromToken(token);
        UserProfileBO bo = userService.getUserInfo(id);
        UserProfileVO profile = UserProfileVO.convert(bo);
        return ResultTool.success(profile);
    }

    @GetMapping(value = "/{id}")
    @ResponseBody
    public UserVO getUserById(@PathVariable("id") Long id, Model model) {
        UserBO bo = userService.findById(id);
        return UserVO.convert(bo);
    }

    @PutMapping(value = "/update")
    @ResponseBody
    public ResultBO<?> updateAccount(@RequestBody UserProfileBO userProfileDTO) {
        UserProfileBO bo = userService.updateUserProfile(userProfileDTO);
        UserProfileVO profile = UserProfileVO.convert(bo);
        return ResultTool.success(profile);
    }

    @PutMapping("/save")
    @ResponseBody
    public ResultBO<?> saveUser(@RequestBody @Valid UserBO userBO) {
        if(!userService.checkUsernameUnique(userBO)) {
            return ResultTool.error(new ExceptionInfoBO(StatusCode.USERNAME_EXISTS_ERROR));
        }
        try {
            if (userBO.getId() != null) {
                userService.updateUser(userBO);
            } else {
                userService.createUser(userBO);
            }
        } catch (ServiceException e) {
            return ResultTool.error(new ExceptionInfoBO(e));
        }
        return ResultTool.success(userBO);
    }
}
